<template>
  <div
    :id="id"
    :class="className"
    :style="{ height: height, width: width }"
  ></div>
</template>

<script>
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "400px",
    },
    height: {
      type: String,
      default: "400px",
    },
    tableData: {
      type: Array, // 确保传递的数据是数组，或者根据数据类型修改
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart(this.tableData);
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
  methods: {
    initChart(tableData) {
      this.chart = this.$echarts.init(document.getElementById(this.id));

      const pieData = tableData || [];
      const colorArr = [
        "#FF7F50",
        "#87CEFA",
        "#DA70D6",
        "#32CD32",
        "#6495ED",
        "#FFD700",
        "#FF69B4",
        "#BA55D3",
        "#CD5C5C",
        "#40E0D0",
        "#FF6347",
        "#FFA07A",
        "#8A2BE2",
        "#FF1493",
      ];

      this.chart.setOption({
        title: {
          // text: "Traffic Sources",
          left: "center",
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
          textStyle: {
            color: "#fff",
          },
        },
        series: [
          {
            name: "Source",
            type: "pie",
            radius: "75%",
            center: ["50%", "50%"],
            data: pieData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            itemStyle: {
              normal: {
                color(params) {
                  // 动态生成 HSL 颜色，保证每个扇区颜色不同
                  // const hue = (params.dataIndex * 360) / pieData.length; // 不同的色相
                  // return `hsl(${hue}, 70%, 50%)`; // 饱和度和亮度固定
                  // 使用取余运算符让颜色数组循环使用
                  return colorArr[params.dataIndex % colorArr.length];
                },
              },
            },
            label: {
              formatter: "{b|{b}}\n{c|{c}} ({d}%)", // 标签显示名称、值和百分比
              rich: {
                b: {
                  fontSize: 14,
                  color: "#fff",
                  lineHeight: 25,
                },
                c: {
                  fontSize: 12,
                  color: "#fff",
                  lineHeight: 20,
                },
              },
              alignTo: "labelLine", // 动态对齐标签，使左右两侧的标签保持在可视范围内
              overflow: "break", // 防止标签过长时溢出
              bleedMargin: 10, // 防止文字标签与图形过于靠近
            },
            labelLine: {
              show: true,
              length: 20, // 调整导线长度，增加与饼图的距离
              length2: 15, // 第二段导线的长度，适当延长以避免标签遮挡
              lineStyle: {
                color: "#fff",
              },
            },
          },
        ],
        backgroundColor: "#394056",
      });
    },
  },
};
</script>
